package com.prs.crm.action.doc;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.TreeNode;

import com.prs.crm.domain.doc.DeptDocType;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.doc.DeptDocTypeService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class DeptDocTypeAction extends BaseAction {

	private static final long serialVersionUID = -1744482281956014556L;
	private DeptDocTypeService deptDocTypeService;
	private DeptDocType deptDocType;
	private DeptDocType parentDeptDocType;
	private Collection<TreeNode> nodes;
	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;

	private Collection<CodeCommon> codelist;

	@Action("setDeptDocType")
	public String setDeptDocType() {
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"单位文档类型");
		return SUCCESS;
	}

	@Action(value = "loadDeptDocTypeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadDeptDocTypeTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<DeptDocType> docTypes = this.getDeptDocTypeService()
				.getRootDeptDocTypes();
		for (DeptDocType type : docTypes) {
			TreeNode node = new TreeNode();
			node.setId(type.getId().toString());
			node.setText(type.getName());
			nodes.add(node);
			addChildNode(type, node);
		}
		return SUCCESS;
	}

	@Action(value = "saveDeptDocType", results = {
			@Result(name = "error", type = "chain", location = "setDeptDocType"),
			@Result(name = "success", type = "json", params = { "root",
					"deptDocType", "excludeProperties", "childs,deptDocType" }) })
	public String saveDeptDocType() {
		if (deptDocType.getName() == null
				|| "".equals(deptDocType.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (deptDocType.getDeptdoctypecode() == null
				|| "".equals(deptDocType.getDeptdoctypecode().trim())) {
			addActionError("编码不能为空");
			return ERROR;
		}
		try {
			if (deptDocType.getId() != null) {
				deptDocType.setDeptDocType(parentDeptDocType);
			} else {
				if ("n".equals(Encode)) {
					int EncodeRplice = this.getCodeService().getBackRpliceAll(
							"手动编码", "文档类型", deptDocType.getDeptdoctypecode());
					if (EncodeRplice != 0) {
						this.addActionError("编码重复!请重新输入");
						return ERROR;
					}
				}
				if ("n".equals(Encode)) {
					Code codes = new Code();
					codes.setCodeValue(deptDocType.getDeptdoctypecode());
					codes.setEntityName(this.getClass().getName());
					codes.setCodeType("手动编码");
					codes.setCodeMolde("单位文档类型");
					this.getCodeService().save(codes);
					deptDocType.setCode(codes);

				} else {
					CodeCommon Vob = this.getCodeCommonService().getVobject(
							codeCommon.getId());
					Code codes = new Code();
					codes.setEntityName(this.getClass().getName());
					codes.setCodeValue(deptDocType.getDeptdoctypecode());
					Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
					this.codeCommonService.saveOrUpdate(Vob);
					codes.setCodeType(Vob.getCodePlan());
					codes.setCodeMolde("单位文档类型");
					this.getCodeService().save(codes);
					deptDocType.setCode(codes);

				}
			}
			this.getDeptDocTypeService().save(deptDocType);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setDeptDocType(null);
		}
		return SUCCESS;
	}

	@Action(value = "removeDeptDocType", results = { @Result(name = "success", type = "json", params = {
			"root", "prompt" }) })
	public String removeDeptDocType() {
		try {
			this.getDeptDocTypeService().remove(deptDocType);
			this.setPrompt("success");
		} catch (Exception e) {
			// TODO: handle exception
			this.setPrompt("error");
		}
		return SUCCESS;
	}

	private void addChildNode(DeptDocType parent, TreeNode parentNode) {
		if (parent.getChilds() != null) {
			for (DeptDocType type : parent.getChilds()) {
				TreeNode node = new TreeNode();
				node.setId(type.getId().toString());
				node.setText(type.getName());
				parentNode.addChildNode(node);
				addChildNode(type, node);
			}
		}
	}

	@Action("viewDeptDocType")
	public String view() {
		return SUCCESS;
	}

	public DeptDocTypeService getDeptDocTypeService() {
		return deptDocTypeService;
	}

	public void setDeptDocTypeService(DeptDocTypeService deptDocTypeService) {
		this.deptDocTypeService = deptDocTypeService;
	}

	public DeptDocType getDeptDocType() {
		return deptDocType;
	}

	public void setDeptDocType(DeptDocType deptDocType) {
		this.deptDocType = deptDocType;
	}

	public DeptDocType getParentDeptDocType() {
		return parentDeptDocType;
	}

	public void setParentDeptDocType(DeptDocType parentDeptDocType) {
		this.parentDeptDocType = parentDeptDocType;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

}
